from collections import deque
 
# 创建 deque
dq = deque([1, 2, 3])
 
# 高效插入和删除
dq.append(4)      # 在末尾添加
dq.appendleft(0)  # 在开头添加
print(dq)         # 输出：deque([0, 1, 2, 3, 4])


# 列表推导式：占用大量内存
large_list = [x**2 for x in range(1000000)]
 
# 生成器表达式：按需生成数据，节省内存
large_gen = (x**2 for x in range(1000000))


lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = list(set(lst))
print(unique_lst)  # 输出：[1, 2, 3, 4, 5] （顺序可能被打乱）


lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = []
for item in lst:
    if item not in unique_lst:
        unique_lst.append(item)
print(unique_lst)  # 输出：[1, 2, 3, 4, 5]


nested_lst = [[1, 2, 3], [4, 5], [6, 7, 8]]
 
# 方法 1：列表推导式
flat_lst = [item for sublist in nested_lst for item in sublist]
print(flat_lst)  # 输出：[1, 2, 3, 4, 5, 6, 7, 8]
 
# 方法 2：使用 itertools.chain
from itertools import chain
flat_lst = list(chain(*nested_lst))
print(flat_lst)  # 输出：[1, 2, 3, 4, 5, 6, 7, 8]


from collections import Counter
 
lst = [1, 2, 2, 3, 3, 3, 4]
count = Counter(lst)
print(count)  # 输出：Counter({3: 3, 2: 2, 1: 1, 4: 1})

stack = []
stack.append(1)  # 压栈
stack.append(2)
print(stack.pop())  # 弹出元素：2
print(stack)        # 输出：[1]